<?php
namespace app\farm\controller;
use app\AdminRest;
use app\farm\model\ClaimOrder;
use app\farm\model\Farmer;
use app\farm\model\FinanceWater;
use app\farm\model\LandCateText;
use app\farm\model\LandOrder;
use app\farm\model\Machine;
use app\farm\model\Monitor;
use app\farm\model\SendOrder;
use app\farm\model\User;
use app\farm\model\LandCate;
use app\farm\model\Wallet;
use app\shop\model\DistributionCash;
use app\shop\model\DistributionList;
use app\shop\model\StoreGoods;
use longbingcore\wxcore\PospalApi;
use longbingcore\wxcore\WxPay;
use longbingcore\wxcore\YsCloudApi;
use think\App;
use app\farm\model\Farmer as Model;
use think\facade\Db;


class AdminReseller extends AdminRest
{


    protected $model;

    protected $user_model;

    protected $cash_model;

    protected $wallet_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model        = new DistributionList();

        $this->cash_model   = new DistributionCash();

        $this->user_model   = new User();

        $this->wallet_model = new Wallet();

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function resellerList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];

        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['a.user_name','like','%'.$input['name'].'%'];

            $where[] = ['a.mobile','like','%'.$input['name'].'%'];
        }

        $data = $this->model->adminDataList($dis,$input['limit'],$where);

        $list = [

            0=>'all',

            1=>'ing',

            2=>'pass',

            4=>'nopass'
        ];

        foreach ($list as $k=> $value){

            $dis_s = [];

            $dis_s[] =['uniacid','=',$this->_uniacid];

            if(!empty($k)){

                $dis_s[] = ['status','=',$k];

            }else{

                $dis_s[] = ['status','>',-1];

            }

            $data[$value] = $this->model->where($dis_s)->count();

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-29 10:17
     * @功能说明:用户管理列表
     */
    public function userRelationshipList(){

        $input = $this->_param;

        $dis = [

            'uniacid' => $this->_uniacid,

            'is_fx'   => 1
        ];

        $data = $this->user_model->where($dis)->field('id,avatarUrl,nickName,pid,fx_bind_time,fx_cash')->order('fx_bind_time')->paginate($input['limit'])->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['top_info'] = $this->user_model->where(['id'=>$v['pid'],'is_fx'=>1])->field('nickName,avatarUrl')->find();
                //产生收益
                $v['total_fx_cash'] = $this->cash_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('cash');
                //累计提现
                $v['wallte_cash']   = $this->wallet_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('pay_price');

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-29 10:49
     * @功能说明:分销关系查找
     */
    public function userRelationshipListFind(){

        $input = $this->_param;

        $dis = [

            'is_fx' => 1
        ];

        if($input['type']==1){
            //自己
            $dis['id'] = $input['user_id'];

        }elseif ($input['type']==2){
            //下级
            $dis['pid'] = $input['user_id'];

        }else{
            //上级
            $top_id = $this->user_model->where(['id'=>$input['user_id'],'is_fx'=>1])->value('pid');

            $dis['id'] = $top_id;
        }

        $data = $this->user_model->where($dis)->field('id,avatarUrl,nickName,pid,fx_bind_time,fx_cash')->order('fx_bind_time')->paginate($input['limit'])->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //产生收益
                $v['total_fx_cash'] = $this->cash_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('cash');
                //累计提现
                $v['wallte_cash']   = $this->wallet_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('pay_price');

            }

        }

        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:58
     * @功能说明:分销商详情
     */
    public function resellerInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $user_model = new User();

        $data['nickName'] = $user_model->where(['id'=>$data['user_id']])->value('nickName');

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-07-03 00:15
     * @功能说明:审核(2通过,3取消,4拒绝)
     */
    public function resellerUpdate(){

        $input = $this->_input;

        $diss = [

            'id' => $input['id']
        ];

        $info = $this->model->dataInfo($diss);

        if(!empty($input['status'])&&in_array($input['status'],[2,4,-1])){

            $input['sh_time'] = time();

            if($input['status']==-1){

                $fx_cash = $this->user_model->where(['id'=>$info['user_id']])->sum('fx_cash');

                if($fx_cash>0){

                    $this->errorMsg('分销商还有佣金未提现');
                }

                $dis = [

                    'user_id' => $info['user_id'],

                    'status'  => 1
                ];

                $cash = $this->cash_model->dataInfo($dis);

                if(!empty($cash)){

                    $this->errorMsg('分销商还有佣金未到账');

                }

                $dis['type'] = 5;

                $wallet = $this->wallet_model->dataInfo($dis);

                if(!empty($wallet)){

                    $this->errorMsg('分销商还有提现未处理');

                }

            }
        }

        $data = $this->model->dataUpdate($diss,$input);

        if(isset($input['status'])){

            $update = [

                'is_fx' => 0
            ];

            if($input['status']==2){

                $update['fx_bind_time'] = time();

               // $update['pid']   = $info['pid'];

                $update['is_fx'] = 1;

            }

            $this->user_model->dataUpdate(['id'=>$info['user_id']],$update);

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-29 11:30
     * @功能说明:分销佣金详情
     */
    public function cashList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.user_name','like','%'.$input['name'].'%'];

            $where[] = ['c.nickName','like','%'.$input['name'].'%'];

        }

        $data = $this->cash_model->adminCashList($dis,$where,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['source_info'] = $this->user_model->where(['id'=>$v['source_id']])->field('nickName,avatarUrl')->find();

                $v['order_price'] = array_sum(array_column($v['order_goods'],'pay_price'));

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-29 14:48
     * @功能说明:用户收益列表
     */
    public function userProfitList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['a.create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['a.nickName','like','%'.$input['name'].'%'];

            $where[] = ['b.user_name','like','%'.$input['name'].'%'];

        }

        $data = $this->model->userProfitList($dis,$input['limit'],$where);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //产生收益
                $v['total_fx_cash'] = $this->cash_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('cash');
                //未入账
                $v['unrecorded_fx_cash'] = $this->cash_model->where(['user_id'=>$v['id']])->where('status','in',[1])->sum('cash');
                //累计提现
                $v['total_wallte_cash']   = $this->wallet_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('pay_price');
                //提现中
                $v['wallte_ing_cash']   = $this->wallet_model->where(['user_id'=>$v['id']])->where('status','in',[1])->sum('pay_price');

            }

        }

        return $this->success($data);
    }










}
